﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace loginapp
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public Form2(string user,string pwd)
        {
            InitializeComponent();
            this.user = user;
            this.pwd = pwd;
        }

        private string user;
        private string pwd;
        private void Form2_Load(object sender, EventArgs e)
        {
            web.Url = new Uri("http://lgdy.whut.edu.cn/index.php?c=home&a=login");

        }

        private void click()
        {
            if (web.Document != null)
            {
                HtmlElementCollection dhl = web.Document.GetElementsByTagName("button");//GetElementsByTagName得到的是点击的标记元素名称
                foreach (HtmlElement item in dhl)
                {
                    if (item.InnerText == "登　录")
                    {
                        item.InvokeMember("click");

                        break;
                    }

                }
            }
        }

        public void fix()
        {
            web.Url = new Uri("http://lgdy.whut.edu.cn/index.php?c=home&a=login");
            web.Refresh();
        }

        private void web_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (string.IsNullOrEmpty(user) || string.IsNullOrEmpty(pwd)) return;
            if (web.Document != null)
            {
                HtmlElementCollection dhl = web.Document.GetElementsByTagName("button");//GetElementsByTagName得到的是点击的标记元素名称
                foreach (HtmlElement item in dhl)
                {
                    if (item.InnerText == "学号／职工号登录")
                    {
                        //MessageBox.Show("ok");
                        item.InvokeMember("click");
                        var elementById = web.Document.GetElementById("username");
                        if (elementById != null)
                            elementById.InnerText = user;
                        var htmlElement = web.Document.GetElementById("password");
                        if (htmlElement != null)
                            htmlElement.InnerText = pwd;
                        var byId = web.Document.GetElementById("remember");
                        if (byId != null)
                            byId.InvokeMember("click");
                        click();


                        break;
                    }

                }
            }
        }
    }
}
